var form = layui.form, laydate = layui.laydate, upload = layui.upload, table = layui.table, $ = layui.jquery;
layui.use(['tableEdit', 'table', 'form', 'laydate',"selectInput"], function () {
    let tableEdit = layui.tableEdit,
        layer = layui.layer,
        laydate = layui.laydate,
        selectInput = layui.selectInput;

    //仓库
    let warehouse = [];
    layer.ready(function () {
        //获取仓库信息
        $.ajax({
            url:"transfer/getWarehouse",
            success:function(data){
                if(data.status == 200){
                    warehouse = data.data;
                    for(let i in warehouse){
                        $('#warehouseCode').append(new Option(warehouse[i].name, warehouse[i].code));
                    }
                    form.render('select');
                }
            }
        });
    })
    form.render('select');

    //监听仓库下拉框
    form.on('select(warehouseCode)', function (data) {
        if(data.value === ""){
            return;
        }
        for(let i in warehouse){
            if(data.value == warehouse[i].code){
                $("#warehouseName").val(warehouse[i].name);
            }
        }
    })

    //海外仓库租金总额改变时,重新计算每天分摊金额和剩余金额
    $("#totalRent").keyup(function(){
        //海外仓库租金总额
        let totalRent = $("#totalRent").val();
        if(!isNumber(totalRent)){
            layer.msg('请输入数字');
            return;
        }
        //分摊天数
        let apportionedDays = $("#apportionedDays").val();
        if(apportionedDays === "0"){
            return;
        }
        //每天分摊金额 = 海外仓库租金总额/分摊天数
        let dailyApportionmentAmount = new BigNumber(totalRent).dividedBy(apportionedDays).dp(4, BigNumber.ROUND_HALF_UP).toString();
        //更新每天分摊金额
        $("#dailyApportionmentAmount").val(dailyApportionmentAmount);

        //剩余金额默认等于海外仓库租金总额
        $("#balance").val(totalRent);
    });

    //分摊天数改变时,重新计算每天分摊金额和剩余金额
    $("#apportionedDays").keyup(function(){
        //分摊天数
        let apportionedDays = $("#apportionedDays").val();
        if(!isNumber(apportionedDays)){
            layer.msg('请输入数字');
            return;
        }
        //海外仓库租金总额
        let totalRent = $("#totalRent").val();
        if(totalRent === "0"){
            return;
        }
        //每天分摊金额 = 海外仓库租金总额/分摊天数
        let dailyApportionmentAmount = new BigNumber(totalRent).dividedBy(apportionedDays).dp(4, BigNumber.ROUND_HALF_UP).toString();
        //更新每天分摊金额
        $("#dailyApportionmentAmount").val(dailyApportionmentAmount);

        //剩余金额默认等于海外仓库租金总额
        $("#balance").val(totalRent);
    });

    //监听提交
    form.on('submit(form-element)', function (data) {
        //向后台组装数据
        //获取头部表单数据
        let req = data.field;
        let datas = JSON.stringify(req);
        $.ajax({
            type: "POST",
            dataType: "json",
            contentType: 'application/json',
            data: datas,
            url: "stock/add/rentalFee",
            success: function (res) {
                if (res.status == 200) {
                    layer.msg("保存成功", {icon: 6});
                    setTimeout(function () {
                        //保存成功则刷新提交过来的界面 并关闭此页面
                        parent.detab("views/jsp/WarehouseManagement/overseasWarehouseRentalFee.jsp");
                        parent.intab("views/jsp/WarehouseManagement/overseasWarehouseRentalFee.jsp", "海外仓库租金费设置")
                        parent.detab("views/jsp/WarehouseManagement/tpl/newRentalFee.jsp");
                    },1000);
                    return false;
                } else {
                    layer.msg(res.msg, {icon: 5});
                }
            }
        });
    });


    //时间处理 获取当前时间type:1 yyyy-MM-dd type:2 yyyyMMdd+毫秒
    function getNowFormatDate(type) {
        let date = new Date();
        let separator = "-";
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        if (1 == type) {
            let currentDate = year + separator + month + separator + strDate;
            return currentDate;
        } else if (2 == type) {
            let second = date.getMilliseconds().toString();
            if (second.length < 3) {
                if (second.length == 1) {
                    second = "0" + "0" + second;
                } else {
                    second = "0" + second;
                }
            }
            let currentDate = year.toString() + month.toString() + strDate.toString() + second;
            return currentDate;
        }

    }

    let currentDate = getNowFormatDate(1);
    //执行日期
    laydate.render({
        elem: '#executionDate',
        value: currentDate,
        isInitValue: true
    });


    //提示信息渲染
    function confirmInfo(icon, anim) {
        let data = {
            icon: icon,
            title: "提示",
            offset: "auto",
            skin: 'layui-layer-molv',
            anim: anim
        }
        return data;
    }

    function isNumber(val) {
        let regPos = /^\d+(\.\d+)?$/; //非负浮点数
        let regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
        if (regPos.test(val) || regNeg.test(val)) {
            return true;
        } else {
            return false;
        }
    }
});